<template>
	<view>
		<view class="bgcolor border-radius-bottom"></view>
		<view class="con" style="margin-top:-30px;padding-bottom:120rpx;">
			<view class="titleview ">
				<view>上传照片</view>
				<view class="dflex-a margin-top-sm">
					<view @click="front" class="dflex-flow-c dflex">
						<view class="borderimg border dflex-c margin-bottom-xs">
							<image v-if="front_img" class="borderimg" :src="front_img.url"></image>
							<image v-else mode="widthFix" class="imglh" src="/static/upimg.png"></image>
						</view>
						正面
					</view>
					<view  @click="side"  class="dflex-flow-c dflex">
						<view class="borderimg border dflex-c margin-bottom-xs">
							<image v-if="side_img" class="borderimg" :src="side_img.url"></image>
							<image v-else mode="widthFix" class="imglh" src="/static/upimg.png"></image>
						</view>
						侧面
					</view>
					<view @click="back" class="dflex-flow-c dflex">
						<view class="borderimg border dflex-c margin-bottom-xs">
							<image v-if="back_img" class="borderimg" :src="back_img.url"></image>
							<image v-else mode="widthFix" class="imglh" src="/static/upimg.png"></image>
						</view>
						后面
					</view>
					
				</view>
				<view class="topred margin-top-lg">*注意事项</view>
				<image mode="widthFix" class="w100 margin-top-xs" src="https://mp-11e7fbe6-59e1-4cd3-894a-1405ba0c7314.cdn.bspapp.com/ai/info.png"></image>
			</view>
		</view>
		<view @click="tapstart" class="start dflex-c">
			开始量体
		</view>
		<use-popup v-model="show" bgclass=" " centerStyle="" borderRadius="20" mode="center">
			<image mode="widthFix" class="dflex" src="https://mp-11e7fbe6-59e1-4cd3-894a-1405ba0c7314.cdn.bspapp.com/ai/donghua.gif"></image>
		</use-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				show:false,
				front_img:'',
				side_img:'',
				back_img:'',
			}
		},
		methods: {
			tapstart(){
				
				if(!this.front_img){
					uni.showToast({
						title:"上传正面图",
						icon:"none"
					})
					return
				}
				if(!this.side_img){
					uni.showToast({
						title:"上传侧面图",
						icon:"none"
					})
					return
				}
				if(!this.back_img){
					uni.showToast({
						title:"上传背面图",
						icon:"none"
					})
					return
				}
				
				var info = uni.getStorageSync('aiInfo')
				info.front_img= this.front_img
				info.side_img= this.side_img
				info.back_img= this.back_img
				info.user_id= uni.getStorageSync('usemall_member')._id
				 uni.showLoading()
				this.$func.usemall.call('aibody/index/setinfo',{
					all:info
				}).then(res=>{
					uni.showToast({
						title:"提交成功",
						icon:"none"
					})
				})
				
				// this.show=true
			},
			chooseImage(type) {
				let _this = this;
				uni.chooseImage({
					count: 1,
					// 可以指定是原图|压缩图，默认二者都有
					sizeType: ['original', 'compressed'],
					success: async (res) => {
						uni.showLoading({
							title: '上传中',
							mask: true
						});
						
						for (var i = 0; i < res.tempFilePaths.length; i++) {
							let filename = res.tempFilePaths[i];
							if (res.tempFiles[i] && res.tempFiles[i].name) {
								filename = res.tempFiles[i].name;
							}
							const result = await uniCloud.uploadFile({
								filePath: res.tempFilePaths[i],
								cloudPath: this.$api.getFileName(filename),
								onUploadProgress: function(progressEvent) {
									var percentCompleted = Math.round((progressEvent.loaded *
										100) / progressEvent.total);
								}
							}).then(async uRes => {
								uRes.url = uRes.fileID;
								// 兼容腾讯云上传文件
								if (uRes.url.indexOf('cloud://') != -1)
								{
									const tempRes = await uniCloud.getTempFileURL({
										fileList: [uRes.url]
									});
									
									if (tempRes.fileList && tempRes.fileList.length > 0) {
										uRes.url = tempRes.fileList[0].tempFileURL;
									}
								}
								
								uRes.type = res.tempFiles[i].type || 'image/';
								uRes.size = res.tempFiles[i].size;
								if(type=='front'){
									_this.front_img = uRes
								}else if(type=='side'){
									_this.side_img = uRes
								}else{
									_this.back_img = uRes
								}
								 
							}).catch(err => {
								uni.showToast({
									title:"上传失败，重新上传",
									icon:"none"
								})
								console.log('use-upload', err);
							});
						}
			
						uni.hideLoading();
					}
				});
			},
			front(){
				this.chooseImage('front')
			},
			side(){
				this.chooseImage('side')
			},
			back(){
				this.chooseImage('back')
			}
		}
	}
</script>

<style>
	.start{
		position: fixed;
		width:100%;
		bottom: 0;
		color:#fff;
		font-weight: bold;
		height: 90rpx;
		background: #00724C;
	}
	.w100{
		width: 100%;
	}
	.topred{
		font-size: 24rpx;
		color:red;
	}
	.imglh{
		width: 140rpx;
	}
	.borderimg{
		width: 168rpx;
		height: 300rpx;
		border-radius: 16px;
	}
	.border{
		border:2px solid #8E8E8E;
	}
	.con {
		width: 92%;
		margin: auto;
	}

	.titleview {
		border-radius: 10px;
		padding: 10px;
		background: #fff;
	}

	.bgcolor {
		background: #00724C;
		height: 44px;
	}
</style>